﻿// MIT License
//
// Copyright (c) 2021-present zuohuaijun, Daming Co.,Ltd and Contributors
//
// 电话/微信：18020030720 QQ群1：87333204 QQ群2：252381476

using Admin.NET.Application.Const;
using Admin.Net.InformationPlatform.Entity;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using Nest;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Admin.Net.InformationPlatform.Service.Order.Dto;
using SjEntityClass;
using Admin.Net.InformationPlatform.Entity.Order;
using Furion.FriendlyException;

namespace Admin.Net.InformationPlatform.Service.Order;

/// <summary>
/// 报备客户
/// </summary>
[ApiDescriptionSettings(groups: "Order", Name = "客户报备2", Tag = "客户报备", Order = 200)]
public class CustomerRecordingService : IDynamicApiController, ITransient
{
    /// <summary>
    /// 单例数据链接
    /// </summary>
    ISqlSugarClient db;
    /// <summary>
    /// T100数据库
    /// </summary>
    private SqlSugarProvider _T100Db;
    private SqlSugarProvider _SjDb;
    private SqlSugarProvider _plm;
    private SqlSugarProvider _Dilon;


    /// <summary>
    /// 用户信息
    /// </summary>
    TokenUser _User;
    /// <summary>
    /// 报备客户管理模块
    /// </summary>
    /// <param name="db"></param>
    public CustomerRecordingService(ISqlSugarClient db)
    {
        this.db = db;
        this._T100Db = this.db.AsTenant().GetConnection(T100Const.ConfigId);
        this._SjDb = this.db.AsTenant().GetConnection(SJDB.ConfigId);
        this._plm = this.db.AsTenant().GetConnection(PLM.ConfigId);
        this._Dilon = this.db.AsTenant().GetConnection(Dilon.ConfigId);
        // 获取 `Jwt` 存储的信息 企业编号与据点信息
        _User = Util.SjCommonUtil.GetT100UserTokenInfo(_T100Db, _Dilon);

    }

    /// <summary>
    /// 判断 报备信息是否重复
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("/SRM/IsHaveCustomerReport")]
    public async Task<bool> IsHaveCustomerReport(IsHaveReportInput input) {

        if (string.IsNullOrEmpty(input.TAXID) || string.IsNullOrEmpty(input.PRODUCTSLINE)) {
            throw Oops.Oh($"输入税号和产品线不能为空！");
        }
        bool isHave = await _SjDb.Queryable<FilingClient>()
             .Where(it => it.TAXID == it.TAXID)
             .Where(it => it.PRODUCTSLINE == it.PRODUCTSLINE)
             .AnyAsync();


        return isHave;

    }


}
